package StringOperation;
class Solution {

    public String reverseWords(String s) {
        String[] strings = s.split("\\s+");
        int right = strings.length - 1;
        int left = 0;


        while (left < right) {
//            reverseEachWord(strings[left]);
//            reverseEachWord(strings[right]);
            String temp = strings[left];
            strings[left] = strings[right];
            strings[right] = temp;
            left++;
            right--;
        }



//        if ((strings.length & 1) == 1) {
//            reverseEachWord(strings[strings.length / 2]);
//        }
        int count;
        if (strings[strings.length-1].equals("")){
             count= strings.length-1;
        }else {
            count= strings.length;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < count; i++) {
            if (i!=count-1){
                sb.append(strings[i]+" ");
            }
            else  if (!strings[i].equals("")){
                sb.append(strings[i]);
            }
        }

        return sb.toString();

    }


//    public  String reverseEachWord(String s){
//
//        char[]  chars=s.toCharArray();
//        int left=0;
//        int right=s.length()-1;
//        while(left<right){
//            char temp=chars[left];
//            chars[left]=chars[right];
//            chars[right]=temp;
//        }
//        return String.valueOf(chars);
//
//    }

    public static void main(String[] args) {

        Solution solution = new Solution();
//        String s= " hello world!";
        String s= " hello  world  ";

        System.out.println(solution.reverseWords(s));
    }


//        public static void main(String[] args) {
//            String line = "this     is a test sentence.";
//            String[] arrayline1 = line.split("s");
//            String[] arrayline2 = line.split("\\s");
//            String[] arrayline3 = line.split("\\s+");
//            System.out.println("使用's'分隔成了" +arrayline1.length + "部分,分别是:");
//            for (String s : arrayline1) {
//                System.out.print(s + ",");
//            }
//            System.out.println("");
//            System.out.println("使用'\\s'分隔成了" + arrayline2.length + "部分,分别是:");
//            for (String s : arrayline2) {
//                System.out.print(s + ",");
//            }
//            System.out.println("");
//            System.out.println("使用'\\s+'分隔成了" + arrayline3.length + "部分,分别是:");
//            for (String s : arrayline3) {
//                System.out.print(s + ",");
//            }
//        }





}